package com.utils;

import model.BusInfo;
import model.Route;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

public final class DB {
	
	static private SessionFactory sf;
	
	static{
		AnnotationConfiguration config = new AnnotationConfiguration();
		config.addAnnotatedClass(BusInfo.class);
		config.addAnnotatedClass(Route.class);
		sf = config.buildSessionFactory();
	}

	@Override
	protected void finalize() throws Throwable {
		sf.close();
		super.finalize();
	}

	public static Session getSession() {
		return sf.openSession();
	}

	public static void save(Object... objs) {
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		for (Object obj : objs)
			session.persist(obj);
		tx.commit();
		session.close();
	}

}
